package org.rsbot.script.wrappers;

import java.awt.Point;
import org.rsbot.client.Client;
import org.rsbot.client.HardReference;
import org.rsbot.client.Model;
import org.rsbot.client.Node;
import org.rsbot.client.RSAnimable;
import org.rsbot.script.methods.Calculations;
import org.rsbot.script.methods.MethodContext;
import org.rsbot.script.methods.MethodProvider;
import org.rsbot.script.methods.Mouse;
import org.rsbot.script.methods.Nodes;
import org.rsbot.script.methods.Tiles;

public class RSObject extends MethodProvider
{
  private org.rsbot.client.RSObject obj;
  private Type type;
  private int plane;

  public RSObject(MethodContext paramMethodContext, org.rsbot.client.RSObject paramRSObject, Type paramType, int paramInt)
  {
    super(paramMethodContext);
    this.obj = paramRSObject;
    this.type = paramType;
    this.plane = paramInt;
  }

  public RSTile getLocation()
  {
    return new RSTile(this.methods.client.getBaseX() + this.obj.getX() / 512, this.methods.client.getBaseY() + this.obj.getY() / 512, this.plane);
  }

  public RSArea getArea()
  {
    if ((this.obj instanceof RSAnimable)) {
      localObject = (RSAnimable)this.obj;
      RSTile localRSTile1 = new RSTile(this.methods.client.getBaseX() + ((RSAnimable)localObject).getX1(), this.methods.client.getBaseY() + ((RSAnimable)localObject).getY1());

      RSTile localRSTile2 = new RSTile(this.methods.client.getBaseX() + ((RSAnimable)localObject).getX2(), this.methods.client.getBaseY() + ((RSAnimable)localObject).getY2());

      return new RSArea(localRSTile1, localRSTile2, this.plane);
    }
    Object localObject = getLocation();
    return (RSArea)new RSArea((RSTile)localObject, (RSTile)localObject, this.plane);
  }

  public RSObjectDef getDef()
  {
    Node localNode = this.methods.nodes.lookup(this.methods.client.getRSObjectDefLoader(), getID());

    if (localNode != null) {
      if ((localNode instanceof HardReference)) {
        return new RSObjectDef((org.rsbot.client.RSObjectDef)(org.rsbot.client.RSObjectDef)((HardReference)localNode).get());
      }

      if ((localNode instanceof org.rsbot.client.SoftReference)) {
        Object localObject = ((org.rsbot.client.SoftReference)localNode).getReference().get();

        if (localObject != null) {
          return new RSObjectDef((org.rsbot.client.RSObjectDef)localObject);
        }
      }
    }
    return null;
  }

  public int getID()
  {
    return this.obj.getID();
  }

  public RSModel getModel()
  {
    try
    {
      Model localModel = this.obj.getModel();
      if ((localModel != null) && (localModel.getXPoints() != null))
        return new RSObjectModel(this.methods, localModel, this.obj);
    }
    catch (AbstractMethodError localAbstractMethodError) {
    }
    return null;
  }

  public boolean isOnScreen()
  {
    RSModel localRSModel = getModel();
    if (localRSModel == null) {
      return this.methods.calc.tileOnScreen(getLocation());
    }
    return this.methods.calc.pointOnScreen(localRSModel.getPoint());
  }

  public Type getType()
  {
    return this.type;
  }

  public boolean doAction(String paramString)
  {
    RSModel localRSModel = getModel();
    if (localRSModel != null) {
      return localRSModel.doAction(paramString);
    }
    return this.methods.tiles.doAction(getLocation(), paramString);
  }

  public boolean doClick()
  {
    return doClick(true);
  }

  public boolean doClick(boolean paramBoolean)
  {
    RSModel localRSModel = getModel();
    if (localRSModel != null) {
      return localRSModel.doClick(paramBoolean);
    }
    Point localPoint = this.methods.calc.tileToScreen(getLocation());
    if (this.methods.calc.pointOnScreen(localPoint)) {
      this.methods.mouse.move(localPoint);
      if (this.methods.calc.pointOnScreen(localPoint)) {
        this.methods.mouse.click(paramBoolean);
        return true;
      }
      localPoint = this.methods.calc.tileToScreen(getLocation());
      if (this.methods.calc.pointOnScreen(localPoint)) {
        this.methods.mouse.move(localPoint);
        this.methods.mouse.click(paramBoolean);
        return true;
      }
    }

    return false;
  }

  public void doHover()
  {
    RSModel localRSModel = getModel();
    if (localRSModel != null) {
      localRSModel.hover();
    } else {
      Point localPoint = this.methods.calc.tileToScreen(getLocation());
      if (this.methods.calc.pointOnScreen(localPoint))
        this.methods.mouse.move(localPoint);
    }
  }

  public boolean equals(Object paramObject)
  {
    return ((paramObject instanceof RSObject)) && (((RSObject)paramObject).obj == this.obj);
  }

  public int hashCode()
  {
    return this.obj.hashCode();
  }

  public static enum Type
  {
    INTERACTABLE, FLOOR_DECORATION, BOUNDARY, WALL_DECORATION;
  }
}